# Contributor: Eric Molitor <eric@molitor.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
# this is a temp hack package for zig that depends on lld16-libs
pkgname=lld16
pkgver=16.0.6
pkgrel=1
_llvmver=${pkgver%%.*}
pkgdesc="The LLVM Linker"
url="https://llvm.org/"
# cannot link anything and fails
# ld.lld: error: unknown emulation: elf64_s390
# ld.lld: error: src/gn/gn_main.o: could not infer e_machine
# from bitcode target triple s390x-alpine-linux-musl
# also fails hundreds of tests
arch="all !s390x"
license="Apache-2.0"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	clang
	cmake
	compiler-rt
	libedit-dev
	llvm$_llvmver-dev
	llvm$_llvmver-static
	llvm$_llvmver-test-utils
	llvm-libunwind-dev
	patchelf
	samurai
	scudo-malloc
	zlib-dev
	"
checkdepends="gtest-dev bash llvm$_llvmver-test-utils"
subpackages="$pkgname-dbg $pkgname-libs $pkgname-dev $pkgname-doc"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/lld-${pkgver//_/}.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/cmake-${pkgver//_/}.src.tar.xz
	riscv-attributes.patch
	9010-lld-Pass-random.randint-stop-parameter-as-int.patch
	9013-lld-RISCV-Handle-relaxation-reductions-of-more-than-65536-bytes.patch
	"
builddir="$srcdir/lld-${pkgver//_/}.src"
# already works, temp package for zig only
options="!check"

prepare() {
	default_prepare
	mv "$srcdir"/cmake-${pkgver//_/}.src "$srcdir"/cmake
	# modified with patch, but off by a few bytes when read for readelf; actual functionality works
	rm test/ELF/riscv-attributes.s
}

build() {
	CFLAGS="$CFLAGS -O2 -DNDEBUG -g1" \
	CXXFLAGS="$CXXFLAGS -O2 -DNDEBUG -g1" \
	CC=clang CXX=clang++ \
	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_SKIP_INSTALL_RPATH=ON \
		-DBUILD_SHARED_LIBS=ON \
		-DLLVM_INCLUDE_TESTS="$(want_check && echo ON || echo ON)" \
		-DLLVM_EXTERNAL_LIT=/usr/bin/lit \
		-DLLD_BUILT_STANDALONE=ON
	cmake --build build
}

check() {
	ninja -C build check-lld
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	install -Dm644 "$builddir"/docs/ld.lld.1 -t "$pkgdir"/usr/share/man/man1/

	# we have the scudo allocator available, so link lld to it to
	# significantly improve performance, especially in LTO contexts
	patchelf --add-needed libscudo.so "$pkgdir"/usr/bin/lld
}

sha512sums="
8df248785cd77e683f3daf9cab76a4a230305b881a7a844ef644b6a55fee366d5fcca23593ca09945ba8ddae0ae119042afe3f43547c106571e98004dbc7f1e6  lld-16.0.6.src.tar.xz
52ecd941d2d85a5b668caa5d0d40480cb6a97b3bf1174e634c2a93e9ef6d03670ca7e96abb0a60cb33ba537b93d5788754dab1f2f45c1f623f788162717d088b  cmake-16.0.6.src.tar.xz
008f3395c6b70c9a69e5fd1a12b42c1f01992ef7ee5382fca520cf4ed54594706a2ca39b73872e2719818f81d4832686e92244768a98e29c2b462b700c641d2e  riscv-attributes.patch
7c5145c70f958252be60f5ca9e7c4d5b7ab77a7e4c6c2cad528644fa6ecc1d348b4d29c6f968c26cb948caad2372c807afc3ea886600ab7e804ec6935b4dab39  9010-lld-Pass-random.randint-stop-parameter-as-int.patch
1f0527a77d6b147a5d1d809f1a3c4d11d38de13f0cbcdd084328c90f7240e9018e727ead370b24f477ee2de9e3cc7ecaee1dc27d29e1f67778d30830c37dbf3c  9013-lld-RISCV-Handle-relaxation-reductions-of-more-than-65536-bytes.patch
"
